package com.ciaojian.store.controller.franchisees;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciaojian.core.mapper.CheckUserMapper;
import com.ciaojian.core.mapper.OrderCommentMapper;
import com.ciaojian.core.mapper.WorkerMapper;
import com.ciaojian.core.mapper.WorkerServiceMapper;
import com.ciaojian.core.model.*;
import com.ciaojian.core.model.vo.*;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.service.OrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "加盟商员工接口")
@RestController
@RequestMapping("/franchisees/worker")
public class FWorkerController {
    @Resource
    private WorkerMapper workerMapper;
    @Resource
    private WorkerServiceMapper workerServiceMapper;
    @Resource
    private OrderCommentMapper orderCommentMapper;

    @ApiOperation("查看发型师服务价格")
    @GetMapping("/getWorkerPrice")
    public Result getWorkerPrice(@RequestParam Integer storeId) {
        QueryWrapper<Worker> workerQueryWrapper = new QueryWrapper<>();
        workerQueryWrapper.eq("store_id",storeId);
        workerQueryWrapper.ne("status",8);
        workerQueryWrapper.eq("is_remove",0);
        List<Worker> workers = workerMapper.selectList(workerQueryWrapper);

        List<WorkerpriceListVo> workerpriceListVos = new ArrayList<>();
        for (Worker worker : workers) {
            WorkerpriceListVo workerpriceListVo = new WorkerpriceListVo();
            workerpriceListVo.setWorkerId(worker.getId());
            workerpriceListVo.setAvatar(worker.getAvatar());
            workerpriceListVo.setName(worker.getName());
            workerpriceListVo.setType(worker.getType().getValue());

            List<WorkerPriceVo> workerPriceVoNew = workerMapper.selectWorkerPrice(worker.getId());
            workerpriceListVo.setService(workerPriceVoNew);
            workerpriceListVos.add(workerpriceListVo);
        }

        return Result.ok(workerpriceListVos);
    }

    @ApiOperation("修改发型师服务价格")
    @PostMapping("/changeWorkerPrice")
    public Result changeWorkerPrice(@RequestBody WorkerPriceVo workerPriceVo) {
        WorkerService workerService = workerServiceMapper.selectById(workerPriceVo.getWorkerServiceId());
        workerService.setPrice(workerPriceVo.getPrice()*100);
        workerService.updateById();
        return Result.ok();
    }



    @ApiOperation("查看加盟店员工")
    @GetMapping("/getWorkerInfo")
    public Result getWorkerInfo(@RequestParam Integer workerId) {
        FranchiseesWorkerVo franchiseesWorkerVo = workerMapper.getWorkerInfo(workerId);
        QueryWrapper<WorkerService> workerServiceQueryWrapper = new QueryWrapper<>();
        workerServiceQueryWrapper.eq("worker_id",workerId);
        List<WorkerService> workerService = workerServiceMapper.selectList(workerServiceQueryWrapper);
        for (WorkerService service : workerService) {
            HaircutService haircutService = new HaircutService().selectById(service.getServiceId());
            service.setServiceName(haircutService.getTitle());
        }

        franchiseesWorkerVo.setServiceList(workerService);
        return Result.ok(franchiseesWorkerVo);
    }

    @ApiOperation("编辑加盟店员工信息")
    @PostMapping("/updateWorkerInfo")
    public Result updateWorkerInfo(@RequestBody FranchiseesWorkerVo franchiseesWorkerVo) {
        Worker worker = workerMapper.selectById(franchiseesWorkerVo.getId());
        worker.setName(franchiseesWorkerVo.getName());
        worker.setNameEn(franchiseesWorkerVo.getNameEn());
        worker.setType(franchiseesWorkerVo.getType());
        worker.setLevelId(franchiseesWorkerVo.getLevelId());
        if (worker.getLevelId()==1){
            worker.setLevelTitle("星级发型师");
        }else if (worker.getLevelId()==2){
            worker.setLevelTitle("资深发型师");
        }else if (worker.getLevelId()==3){
            worker.setLevelTitle("高级发型师");
        }
        worker.setTelphone(franchiseesWorkerVo.getTelphone());
        worker.updateById();

        if (franchiseesWorkerVo.getServiceList().size()>0){
            for (WorkerService workerService : franchiseesWorkerVo.getServiceList()) {
                workerService.updateById();
            }
        }
        return Result.ok();
    }

    @ApiOperation("员工状态修改(离职")
    @PostMapping("/updateWorkerStatus")
    public Result updateWorkerStatus(@RequestBody FranchiseesWorkerVo franchiseesWorkerVo) {
        Worker worker = workerMapper.selectById(franchiseesWorkerVo.getId());
        worker.setStatus(franchiseesWorkerVo.getStatus());
        worker.updateById();
        return Result.ok();
    }


    @ApiOperation("员工评价列表")
    @GetMapping("/workerComment")
    public Result workerComment(@RequestParam Integer workerId, @RequestParam(required=false)Integer
            score, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {

        pageNum = (pageNum-1) * pageSize;
        int total = orderCommentMapper.getDEvaluateCount(workerId,score,pageNum,pageSize,startDate,endDate);
        List<OrderCommentVo> dEvaluate = orderCommentMapper.findDEvaluate(workerId,score,pageNum,pageSize,startDate,endDate);
        for (OrderCommentVo d : dEvaluate) {
            Integer firstMoney = orderCommentMapper.getFirstMoney(d.getUserId());
            if(firstMoney > 1){
                d.setFirstStatus(1);
            } else {
                d.setFirstStatus(0);
            }
        }

        List<Map<String,Object>> scores = orderCommentMapper.scoresGroup(workerId,startDate,endDate);

        OrderCommentPageVo orderCommentPageVo = new OrderCommentPageVo();
        orderCommentPageVo.setOrderCommentList(dEvaluate);
        orderCommentPageVo.setPageNum(pageNum);
        orderCommentPageVo.setPageSize(pageSize);
        orderCommentPageVo.setTotal(total);
        orderCommentPageVo.setScoresGroup(scores);

        return Result.ok(orderCommentPageVo);
    }



}
